const axios = require('axios');

// 请求拦截
axios.interceptors.request.use(function(config) {
  // Do something before request is sent
  config.headers['x-csrf-token'] = window.__INITIAL_STATE__['csrf'];
  return config;
}, function(error) {
  // Do something with request error
  return Promise.reject(error);
});

// 返回 拦截
axios.interceptors.response.use(function(response) {
  // Do something with response data
  return Promise.resolve(response);
}, function(error) {
  // Do something with response error
  const data = error.response.data, constructor = data.constructor;
  return Promise.resolve(new Error(error.response.data || '与服务器通讯失败'));
});

export default axios;